# -*- coding: utf-8 -*-
"""
Created on Fri Jan 29 15:13:12 2016

@author: jdh1
"""

# version 13_paolo 

# - class structure
# - added input & exec screens
# - solved some issues with button2
# - should have exactly the same features as version 13 (no regressions)
# - TODO: data export
# - CRUCIAL: WHICH data do we want to export?

import wx      # this is the GUI interface
import numpy as np
from numpy import genfromtxt

class AspirationDialog(wx.Dialog):
    def __init__(self, parent, id):
        wx.Dialog.__init__(self, parent, id,  style=wx.CAPTION)
    
        self.parent = parent
        #elements setup
        self.Description = wx.StaticText(self,  -1,  u'Please enter your aspiration level\n\nThis will cost you %s ECU.'%self.parent.k,  (10, 10))
        self.Aspiration = wx.TextCtrl(self, -1, u'', style=wx.ALIGN_LEFT)
        self.Description.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
        #self.Description.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, 0, ""))        
        self.SubmitButton = wx.Button(self, -1, u'OK', size=(120, 30))
        self.CancelButton = wx.Button(self, -1, u'Cancel', size=(120, 30))
        
        self.SubmitButton.Bind(wx.EVT_BUTTON, self.OnSubmit)
        self.CancelButton.Bind(wx.EVT_BUTTON, self.OnCancel)
        
        self.__do_layout()
        
    def __do_layout(self):
#        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox1.Add(self.Description,  wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT,  20)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2.Add(self.SubmitButton, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT,  20)
        hbox2.Add(self.CancelButton, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT,  20)
#        vbox.Add(panel)
        vbox.Add(hbox1, 0, wx.ALIGN_CENTER | wx.ALL, 10)
        vbox.Add(self.Aspiration,1,wx.ALIGN_CENTER | wx.ALL, 10)
        vbox.Add(hbox2, 1, wx.ALIGN_CENTER | wx.ALL, 10)
        self.SetSizer(vbox)
        self.Fit()

    def OnSubmit(self,  event):
        try: 
            a = int(self.Aspiration.GetValue())
            if a:
                self.parent.Aspiration = a
                self.parent.ActionCanceled = 0
                self.Destroy()
        except:
            wx.MessageDialog(self, u'Make sure to have entered a number!', u'Not a number',  wx.OK|wx.LEFT|wx.ICON_EXCLAMATION).ShowModal()
            
    def OnCancel(self, event):
        self.parent.ActionCanceled = 1
        self.Destroy()


class InputScreen(wx.Frame):
    """Allowing the experimenter to input the subject number """
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, None, -1, **kwds)
        #init panel
        self.panel = wx.Panel(self,-1)
        
        #texts
        self.welcome = wx.StaticText(self.panel,  -1,  'Input the subject number and the parameter set')        
        self.IdText = wx.StaticText(self.panel,  -1,  "Subject number:")
        self.ParamText = wx.StaticText(self.panel,  -1,  "parameter set:")
        
        #inputs
        self.Idctrl = wx.TextCtrl(self.panel, -1, u'', style=wx.ALIGN_LEFT)
        self.Param = wx.TextCtrl(self.panel, -1, u'', style=wx.ALIGN_LEFT)
        
        self.welcome.SetFont(wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        self.IdText.SetFont(wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD,  0, ""))
        self.ParamText.SetFont(wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD,  0, ""))
        
                
        #button (in the interace, on the bottom of the screen. You cannot see it but it is there)
        self.button = wx.Button(self.panel, -1, 'Done. Go to waiting screen.', size=(120, 30), style=wx.NO_BORDER)
        self.button.Bind(wx.EVT_BUTTON,  self.OnActivation)
        
        #display all the widgets in the panel/window        
        self.__do_layout()

    def __do_layout(self):
        """This function generates the layout of the screen using sizer"""
        self.grid = wx.GridBagSizer(hgap=5, vgap=5)

        centerup = wx.BoxSizer(wx.VERTICAL)
        centerup.Add(self.welcome, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 1)
        
        centerdown = wx.BoxSizer(wx.VERTICAL)
        centerdown.Add(self.button,  1,  wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        
        self.grid.Add(centerup, pos=(0, 0),  flag=wx.EXPAND|wx.ALIGN_CENTRE)
        self.grid.Add(self.IdText, pos=(2, 0),  flag=wx.EXPAND |wx.ALIGN_RIGHT)
        self.grid.Add(self.Idctrl, pos=(2, 1),  flag=wx.EXPAND |wx.ALIGN_CENTRE)
        self.grid.Add(self.ParamText, pos=(3, 0),  flag=wx.EXPAND |wx.ALIGN_RIGHT)
        self.grid.Add(self.Param, pos=(3, 1),  flag=wx.EXPAND |wx.ALIGN_CENTRE)
        self.grid.Add(centerdown, pos=(4, 0), span=(1, 4),  flag=wx.EXPAND |wx.ALIGN_CENTRE)
        self.grid.AddGrowableRow(0,  proportion = 3)
        self.grid.AddGrowableRow(4,  proportion = 1)
        self.grid.AddGrowableCol(1)
        
        self.panel.SetSizer(self.grid)
        
    def OnActivation(self, event):
        """What happens when you click on the bottom button?"""
        # store the subjectID and the parameter set
        global subjectID
        global parameterSet
        subjectID = int(self.Idctrl.GetValue())
        parameterSet = int(self.Param.GetValue())
        waitscreen.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)
        self.Destroy()

class WaitScreen(wx.Frame):
    """Exec waiting screen. Nothing fancy. it incorporates a button for starting the game."""
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, None, -1, **kwds)
        #init panel
        self.panel = wx.Panel(self,-1)
        
        #texts
        self.welcome = wx.StaticText(self.panel,  -1,  'Welcome to this experiment')        
        self.waittext = wx.StaticText(self.panel,  -1,  "\nPlease wait")
        
        self.welcome.SetFont(wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        self.waittext.SetFont(wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.BOLD,  0, ""))
        
        #exec image        
        self.execimg = wx.Image('exec.png', wx.BITMAP_TYPE_PNG)
        self.w = self.execimg.GetWidth()
        self.h = self.execimg.GetHeight()
        self.execimg2 = self.execimg.Scale(self.w/0.5, self.h/0.5)
        self.execimg2 = self.execimg2.ConvertToBitmap()
        self.img = wx.StaticBitmap(self.panel, -1, self.execimg2)
        
        #button (in the interace, on the bottom of the screen. You cannot see it but it is there)
        self.button = wx.Button(self.panel, -1, '', size=(120, 30), style=wx.NO_BORDER)
        self.button.Bind(wx.EVT_BUTTON,  self.OnActivation)
        
        #display all the widgets in the panel/window        
        self.__do_layout()

    def __do_layout(self):
        """This function generates the layout of the screen using sizer"""
        self.grid = wx.GridBagSizer(hgap=5, vgap=5)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        centerup = wx.BoxSizer(wx.VERTICAL)
        centerup.Add(self.welcome, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 1)
        
        centermiddle = wx.BoxSizer(wx.VERTICAL)
        centermiddle.Add(self.img, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 1)
        centermiddle.Add(self.waittext, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        hbox.Add(centermiddle,  1,  wx.EXPAND|wx.ALL, 5)
        
        centerdown = wx.BoxSizer(wx.VERTICAL)
        centerdown.Add(self.button,  1,  wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        
        self.grid.Add(centerup, pos=(0, 1),  flag=wx.EXPAND|wx.ALIGN_CENTRE)
        self.grid.Add(hbox, pos=(2, 1),  flag=wx.EXPAND |wx.ALIGN_CENTRE)
        self.grid.Add(centerdown, pos=(4, 0), span=(1, 3),  flag=wx.EXPAND |wx.ALIGN_CENTRE)
        self.grid.AddGrowableRow(0,  proportion = 3)
        self.grid.AddGrowableRow(2,  proportion = 6)
        self.grid.AddGrowableRow(4,  proportion = 1)
        self.grid.AddGrowableCol(1)
        
        self.panel.SetSizer(self.grid)
        
    def OnActivation(self, event):
        """What happens when you click on the bottom button?
           at the moment, it just destroys the window. 
           might be useful to make him ShowFullScreen() the main experiment window."""
        inputfile = 'input'+str(parameterSet)+'.csv'       
        win = MainScreen(inputfile = inputfile, subjectID = subjectID, title="Experiment",size=(screenwidth,screenheight))
        win.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)
        win.SetBackgroundColour("White")
        self.Destroy()
        
class MainScreen(wx.Frame):
    """Main Experiment screen"""
    def __init__(self, inputfile, subjectID, *args, **kwds):
        wx.Frame.__init__(self, None, -1, **kwds)
        
        self.panel = wx.Panel(self,-1)
        
        self.exptfont = wx.Font(16, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Calibri')
        
        self.inputfile = inputfile
        self.subjectID = subjectID

        # reading in the parameters of the problems - in the order N, L, U, k, x, D and K
        self.parameters = genfromtxt(inputfile, delimiter=',')
        self.nprobs = sum(1 for row in self.parameters)   
        
        self.lvgeal=0
        self.costs=0
        #netpayoffs
        self.netpayoffs = [0]*self.nprobs
        #print("netpayoffs are "+str(netpayoffs))
        
        gbb=25*xs  # gap between the buttons
        wb=75*xs   # width of buttons
        lhm=(screenwidth-2*gbb-3*wb)/2  # left-hand margin
        
        xpinfo=lhm   # information box horizontal
        ypinfo=150*ys # information box vertical
        self.xsinfo=screenwidth-2*lhm # horizontal size of information box
        ysinfo=70*ys # vertical size of information box
        
        xpinst=lhm   # instruction box horizontal
        ypinst=10*ys# instruction box vertical
        xsinst=screenwidth-2*lhm  # horizontal size of instruction box
        ysinst=120*ys             # vertical size of instruction box
        
        self.xpvals=lhm   # information box horizontal
        self.ypvals=135*ys # information box vertical
        self.xsvals=screenwidth-2*lhm # horizontal size of information box
        self.ysvals=10*ys # vertical size of information box
        
        xb1=lhm         # first button horizontal 
        xb2=xb1+wb+gbb  # second button horizontal 
        xb3=xb2+wb+gbb  # third button horizontal 
        yb=250*ys       # all three boxes vertical
        xdb=wb          # horizontal size of each button
        ydb=25*ys       # vertical size of each button
        
        #empty data to start with
        self.data = []
        self.b2data = []
        self.Aspiration = 0
        
        
        #  decision=0 ##decision was never used so I commented it away
        self.pn = 1
        np.random.seed(self.pn)
        
        # here we are using the genValues function to generate the values.
        self.N,self.L,self.U,self.k,self.K,self.D,self.x,self.y = self.genValues(self.pn)
        
        
        ### define buttons and all the static things in the interface
                   
        #these are the buttons
        self.button1 = wx.Button(self.panel,-1, label="Click to costlessly exercise at lowest value \n remaining",pos=(xb1,yb-35),size=(xdb,ydb))    
        self.button1.SetBackgroundColour("LightBlue")
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
        
        self.button2 = wx.Button(self.panel,label="Click to ask, at a cost of " +str(self.k)+" ECU, if there \n are payoffs greater than some specified level",pos=(xb2,yb-35),size=(xdb,ydb))
        self.button2.SetBackgroundColour("LightBlue")
        self.button2.Bind(wx.EVT_BUTTON, self.button2Click)
        
        self.button3 = wx.Button(self.panel,label="Click, at cost of "+str(self.K)+" ECU, to find the highest payoff",pos=(xb3,yb-35),size=(xdb,ydb))
        self.button3.SetBackgroundColour("LightBlue")
        self.button3.Bind(wx.EVT_BUTTON, self.button3Click)
        
        self.continueButton = wx.Button(self.panel,label="Click to continue to problem "+str(self.pn+1),pos=(xb2,yb-35),size=(xdb,ydb))
        self.continueButton.SetBackgroundColour("LightBlue")
        self.continueButton.Bind(wx.EVT_BUTTON, self.continueButtonClick)
        self.continueButton.Hide()
        
        self.endButton = wx.Button(self.panel,label="Click to close window",pos=(xb2,yb-60),size=(xdb,ydb))
        self.endButton.SetBackgroundColour("LightBlue")
        self.endButton.SetForegroundColour("Red")
        self.endButton.Bind(wx.EVT_BUTTON, self.endButtonClick)
        self.endButton.Hide()
        
        infotext="""\n\nThis is problem number """+str(self.pn)+""". In this problem, the payoffs are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU."""
        self.information=wx.StaticText(self.panel, -1, infotext,pos=(xpinfo,ypinfo),size=(self.xsinfo,ysinfo),style=wx.TE_MULTILINE)
        self.information.SetFont(self.exptfont)
        self.information.Wrap(self.xsinfo) 
        self.information.SetForegroundColour('black')
        
        
        insttext="""Instructions
        There follow several problems. In each problem there is a set of payoffs (denominated in ECU - experimental currency units). You have to choose a payoff, and the net payoff for that problem will be the payoff that you choose less any costs you have incurred in finding it. Initially you will be told nothing about the payoffs in any one problem, other than that they are between some lower bound and some upper bound. However, you can buy information about the payoffs if you want, but you do not need to. 
        If you choose not to pay, your payoff will simply be the lowest payoff, and that will be the end of that problem. 
        If you pay an amount k (you will be told what it is), and specify some aspiration level y, you will be told if there are any payoffs larger than or equal to y. You can repeat this as many times as you want, but each time you repeat it the amount k will be deducted from the payoff you eventually choose. 
        If you pay an amount K (you will be told what it is), you will be told the highest payoff, and your payoff for that problem will be the highest payoff minus K, and that will be the end of that problem. 
        Your payment for the experiment as a whole will be your show-up fee plus the average net payoff.
        """
        self.instructions=wx.StaticText(self.panel, -1, insttext,pos=(xpinst,ypinst),size=(xsinst,ysinst),style=wx.TE_MULTILINE)
        self.instructions.SetFont(self.exptfont)
        self.instructions.Wrap(xsinst)
        
        
        valuestext=""
        self.values=wx.StaticText(self.panel, -1, valuestext,pos=(self.xpvals,self.ypvals),size=(self.xsvals,self.ysvals),style=wx.TE_MULTILINE)
        self.values.SetFont(self.exptfont)
        self.values.Wrap(self.xsinfo)  

        

    def button1Click(self, event):
        #self.lvgeal=self.y[0]
        if not self.Aspiration:
            self.lvgeal=self.y[0]
        self.netpayoffs[self.pn-1]=self.lvgeal -  self.costs
        if self.D==0:
            infotext="""\n\nThis is problem number """+str(self.pn)+""". In this problem, the payoffs are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU."""  
        else:
            infotext="""\n\nThis is problem number """+str(self.pn)+""". In this problem there are """+str(self.N) +""" payoffs and they are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU."""
        self.insert=wx.StaticText(self.panel,-1, """\n\nYou have decided to stop. The lowest payoff is """ +str(self.lvgeal)+ """. Your net payoff for this problem is therefore """+str(self.lvgeal-self.costs)+""".""",(231.25,550),(100,60),wx.ALIGN_CENTER)
        self.insert.SetForegroundColour(wx.RED)
        self.insert.SetFont(self.exptfont)
        #infotext=infotext+"""\n\nYou have decided to stop. The action with the lowest payoff has payoff """ +str(self.lvgeal)+ """. Your payoff for this problem is therefore """+str(self.lvgeal-self.costs)+"""."""
        self.information.SetLabel(infotext)
        self.information.Wrap(self.xsinfo)
        
                
        self.button1.Hide()
        self.button2.Hide()
        self.button3.Hide()
        
        
        #saving data
        if self.b2data:
            self.data.append( [self.pn] + self.y + self.b2data + ['b1'] + [self.netpayoffs[self.pn-1]])
        else:
            self.data.append( [self.pn] + self.y + ['b1'] + [self.netpayoffs[self.pn-1]])
        
        #preparing for next period
        self.pn = self.pn+1
        self.costs=0
        self.continueButton.Show()
        self.values.SetLabel("")
        #resetting button2 data
        self.b2data = []
    
    def button2Click(self, event):
        #add to data that b2 has been clicked
        self.b2data.append('b2')
        #call dialog
        dlg = AspirationDialog(self, -1)
        dlg.ShowModal()
        
        #if the subject did not cancel
        if self.ActionCanceled:
            self.b2data.append('canceled')
        else:
            #get aspiration level
            self.aspirationlevel = self.Aspiration
            self.b2data.append(self.Aspiration)
            
            #set text to reflect dialog        
            if int(self.y[self.N-1])>=int(self.aspirationlevel):
                valuestext="\nYes, there is at least one payoff greater than or equal to "+str(self.aspirationlevel)+"."
                self.Lnew=int(self.aspirationlevel)
            if int(self.y[self.N-1])<int(self.aspirationlevel):
                valuestext="\nNo, there is no payoff greater than "+str(self.aspirationlevel)+"."
                self.Lnew=self.L 
            self.values.SetLabel(valuestext)
            self.values.SetForegroundColour("red")
            
#            self.lvgeal=self.y[0]
            triggered=0
            print("aspirationlevel is "+str(self.aspirationlevel))
            print("y is "+str(self.y))
            for n in range(1,self.N):
                print("n is "+str(n))
                if triggered==0 and int(self.y[n])>=int(self.aspirationlevel):
                    triggered=1
                    self.lvgeal=self.y[n]
            print("lvgeal is "+str(self.lvgeal))
            self.costs=self.costs+self.k
            print("costs is "+str(self.costs))
            
       
    
    def button3Click(self, event):
#        global N,L,U,k,K,x,y,D,Lnew,values,aspirationlevel,self.lvgeal,self.costs
        if self.D==0:
            infotext="""\n\nThis is problem number """+str(self.pn)+""". In this problem, the payoffs are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU.""" 
        else:
            infotext="""\n\nThis is problem number """+str(self.pn)+""". In this problem there are """+str(self.N) +""" payoffs and they are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU."""
        self.insert=wx.StaticText(self.panel,-1,"""\n\nYou have decided to pay """+str(self.K)+""" ECU to find out the highest payoff. This payoff is """ +str(self.y[self.N-1])+ """ ECU. Your net payoff for this problem is therefore """+str(self.y[self.N-1]-self.costs-self.K)+""" ECU.""",(231.25,540),(100,60),wx.ALIGN_CENTER)
        self.insert.SetForegroundColour(wx.RED)
        self.insert.SetFont(self.exptfont)
        self.insert.Wrap(self.xsinfo)
        #infotext=infotext+"""\n\nYou have decided to pay """+str(self.K)+""" ECU for learning the action with the highest payoff. This payoff is """ +str(self.y[self.N-1])+ """ ECU. Your payoff for this problem is therefore """+str(self.y[self.N-1]-self.costs-self.K)+""" ECU."""  
        
        self.information.SetLabel(infotext)
        self.information.Wrap(self.xsinfo)
        self.button1.Hide()
        self.button2.Hide()
        self.button3.Hide()
        self.netpayoffs[self.pn-1]=self.y[self.N-1]-self.costs - self.K
        
        #saving data
        if self.b2data:
            self.data.append( [self.pn] + self.y + self.b2data + ['b3'] + [self.netpayoffs[self.pn-1]])
        else:
            self.data.append( [self.pn] + self.y + ['b3'] + [self.netpayoffs[self.pn-1]])
        
        self.costs=0
        self.continueButton.Show()
        self.values.SetLabel("")
        self.pn = self.pn+1
        #resetting button2 data
        self.b2data = []
    
    def continueButtonClick(self, event):
        
        #if we are at the last stage
        if self.pn == len(self.parameters)+1:
            #here you put all the things you wish to happen in case we are at the last stage.
            #for the moment, I just delete all buttons and replace the text with a goodbye screen.
            #you can do more (compute final payoff, etc...; up to you)
            #instructions removed
            self.instructions.SetLabel("")
    
            #infotext changed to goodbye - bold
            self.information.SetLabel("The experiment is over.\nYour average net payoff is "+str(sum(self.netpayoffs)/100)+".\nThanks for your participation.")
            self.information.SetFont(wx.Font(26, wx.MODERN, wx.NORMAL, wx.BOLD, False, u'Calibri'))
    
            # continue button hidden
            self.continueButton.Hide()
            print("netpayoffs are "+str(self.netpayoffs))
            self.insert.Hide()
            
            #saving data
            if self.b2data:
                self.data.append( ['Average Net Payoff'] + [sum(self.netpayoffs)/100])
            else:
                self.data.append( ['Average Net Payoff'] + [sum(self.netpayoffs)/100])
            
            #store data
            self.saveData()
            
            # end experiment button shown
            self.endButton.Show()
            
        #if we are NOT at the last stage:
        else:   
            #retrieving the parameters from the data
            self.N,self.L,self.U,self.k,self.K,self.D,self.x,self.y = self.genValues(self.pn)
            #you need to change the labels of the 2 buttons
            self.button2.SetLabel("Click to ask, at a cost of " +str(self.k)+" ECU, if there \n are payoffs greater than some specified level")
            self.button3.SetLabel("Click, at cost of "+str(self.K)+" ECU, to find the highest payoff")
            
            #you need to change the label of the continue button
            #this is conditional on being on the last playing period
            if self.pn == len(self.parameters):
                self.continueButton.SetLabel("Click to go to the end of the experiment")  
            else:
                self.continueButton.SetLabel("Click to continue to problem "+str(self.pn+1))  
                self.lvgeal=self.y[0]
                
                #print("in Continue self.lvgeal is "+str(self.lvgeal))
            #you need to change the text up top as well
            if self.D==0:
                self.information.SetLabel("""\n\nThis is problem number """+str(self.pn)+""". In this problem, the payoffs are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU.""")
                self.information.Wrap(self.xsinfo)
            else:
                self.information.SetLabel("""\n\nThis is problem number """+str(self.pn)+""". In this problem there are """+str(self.N) +""" payoffs and they are between """ +str(self.L)+ """ and """ +str(self.U)+ """ ECU. The cost of finding out whether there are payoffs above some specified (by you) value is """ +str(self.k)+ """ ECU. The cost of finding out the highest payoff is """ +str(self.K) +""" ECU.""")
                self.information.Wrap(self.xsinfo)
            
            #you need to show the buttons again
            self.button1.Show()
            self.button2.Show()
            self.button3.Show()
            
            #hide the continue button
            self.continueButton.Hide()
            self.insert.Hide()
        
    
    #end button action
    def endButtonClick(self, event):
        self.Destroy()
        
    def saveData(self):
        #depending on the data, this writes to a file
        filename = 'subject{0}input{1}.csv'.format(subjectID,parameterSet)
        log = open(filename, 'w')
        for row in self.data:
            print >> log, ','.join([str(w) for w in row])
        log.close()
    
    #update values
    def genValues(self, pn):
        #Print("pn is "+str(pn))
        N=int(self.parameters[pn-1][0])
        L=int(self.parameters[pn-1][1])
        U=int(self.parameters[pn-1][2])
        k=int(self.parameters[pn-1][3])
        K=int(self.parameters[pn-1][4])
        D=int(self.parameters[pn-1][5])
        x=self.parameters[pn-1][6:16]
        v=map(int,x)
        y=sorted(v) 
        return (N,L,U,k,K,D,x,y)
    

#initial setup
class MyApp(wx.App):
    def OnInit(self):
        self.locale=wx.Locale(wx.LANGUAGE_ENGLISH)
        return True
    
app = MyApp()
subjectID = 0
parameterSet = 0
parameterSets=0

#screen parameters
# these are horizontal and vertical scaling
xs=3.7  # this is for controlling the horizontal scaling
ys=2.9  # this is for controlling the vertical scaling
screenwidth=400*xs
screenheight=335*ys

#let's start from the waiting and input screens
waitscreen = WaitScreen()
inputscreen = InputScreen() 
inputscreen.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)     

#starting the app    
app.MainLoop()
